//
//  MyOrderDetailsController.swift
//  Fancyswap
//
//  Created by Cheikh Bara on 2015-04-19.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import UIKit
// import Alamofire

class MyOrderDetailsController: UITableViewController {
    
    @IBOutlet weak var image:    UIImageView!
    @IBOutlet weak var name:     UILabel!
    @IBOutlet weak var brand:    UILabel!
    @IBOutlet weak var fromUser: UILabel!
    @IBOutlet weak var price:    UILabel!
    @IBOutlet weak var status:   UILabel!
    
    @IBOutlet weak var receiverName: UILabel!
    @IBOutlet weak var address:      UILabel!
    @IBOutlet weak var city:         UILabel!
    @IBOutlet weak var state:        UILabel!
    @IBOutlet weak var country:      UILabel!
    @IBOutlet weak var zip:          UILabel!
    
    var order: Orders?
    var orderDetails = OrderDetails()
    
    let ORDERS_STATUS_SUCCESS = 1
    let STATUS_CANCELLED      = 4
    let STATUS_ORDERED        = 3
    let STATUS_WAIT_APPROVAL  = 2
    let STATUS_ACTIVE         = 1
    let STATUS_INACTIVE       = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        loadOrderDetails(order!.id)
    }
    
    override func viewDidAppear(animated: Bool) {
        self.tableView.reloadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 2
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return 1
    }

    func loadOrderDetails(orderId: Int) {
//        Alamofire.
        Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization":fancy_token]
//        Alamofire.
        request(.GET, "http://api.fancyswap.com/rest/1.0/order/\(orderId)")
            .responseJSON { (request, response, JSON_REPONSE, error) in
                
                let JSON_REPONSE: AnyObject = JSON_REPONSE!
                
                if let status = JSON_REPONSE["status"] as? Int {
                    if(status == self.ORDERS_STATUS_SUCCESS) {
                        
                        let orderJson = JSON_REPONSE["order"] as NSDictionary
                        self.orderDetails = OrderDetails(dic: (orderJson as NSDictionary))
                        
                        self.fetchData()
                        
                    } else {
                        let message = JSON_REPONSE["message"] as? String
                        println("Erreur :\(message)")
                    }
                } else {
                    println("Erreur :\(JSON_REPONSE)")
                }
        }
    }

    func fetchData(){
        image.image   = order!.thumbnail
        name.text     = order!.item.name
        brand.text    = order!.item.brand
        fromUser.text = order!.fromUser
        price.text    = String(order!.item.value)
        
        switch order!.status {
            case STATUS_CANCELLED:
                status.text      = "Annulé"
                status.textColor = UIColor.redColor()
            case STATUS_ORDERED:
                status.text      = "Commandé"
                status.textColor = UIColor.blueColor()
            case STATUS_WAIT_APPROVAL:
                status.text      = "En attente"
                status.textColor = UIColor.orangeColor()
            case STATUS_ACTIVE:
                status.text      = "Approuvé"
                status.textColor = UIColor.greenColor()
            case STATUS_INACTIVE:
                status.text      = "Refusé"
                status.textColor = UIColor.redColor()
            default:
            status.text      = ""
        }
        
        receiverName.text = orderDetails.address.firstName + " " + orderDetails.address.lastName
        address.text      = orderDetails.address.address
        city.text         = orderDetails.address.city + ","
        state.text        = orderDetails.address.state
        country.text      = orderDetails.address.country
        zip.text          = orderDetails.address.zip
    }
}
